package com.yedou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.yedou.entity.dto.PageDTO;
import com.yedou.entity.dto.PeopleQuery;
import com.yedou.entity.po.Address;
import com.yedou.entity.po.People;
import com.yedou.entity.vo.AddressVO;
import com.yedou.entity.vo.PeopleVO;
import com.yedou.mapper.PeopleMapper;
import com.yedou.service.IPeopleService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class IPeopleServiceImpl extends ServiceImpl<PeopleMapper, People> implements IPeopleService{
    @Override
    public void deductBalance(Long id, Integer money) {
        People user = this.getById(id);

        // 2.校验用户状态
        if(user == null || user.getStatus() == 2){
            throw new RuntimeException("用户状态异常");
        }
        // 3.校验余额是否充足
        if(user.getBalance() < money){
            throw new RuntimeException("用户余额不足");
        }
        // 4.减扣余额
        int remainBalance = user.getBalance() - money;
//        baseMapper.deductBalance(id, remainBalance);
        this.lambdaUpdate().set(People::getBalance, remainBalance)
                .set(remainBalance==0,People::getStatus,2)
                .eq(People::getId, id)
                .eq(People::getBalance, user.getBalance())
                .update();

    }

    @Override
    public List<People> queryPeoples(String name, Integer status, Integer minBalance, Integer maxBalance) {
//        return baseMapper.queryPeoples(name,status,minBalance,maxBalance);
        List<People> peopleList = lambdaQuery()
                .like(name != null, People::getUsername, name)
                .eq(status != null, People::getStatus, status)
                .ge(minBalance != null, People::getBalance, minBalance)
                .le(maxBalance != null, People::getBalance, maxBalance)
                .list();
        return peopleList;
    }

    @Override
    public PeopleVO queryPeopleById(Long id) {
        People people = this.getById(id);
        if(people != null && people.getStatus() == 2){
            throw new RuntimeException("用户状态异常");
        }
        List<Address> addresses = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, people.getId()).list();
        PeopleVO peopleVO = BeanUtil.copyProperties(people, PeopleVO.class);
        if(CollUtil.isNotEmpty(addresses)){
            List<AddressVO> addressVOS = BeanUtil.copyToList(addresses, AddressVO.class);
            peopleVO.setAddresses(addressVOS);
        }
        return peopleVO;
    }

    @Override
    public PageDTO<PeopleVO> queryUsersByPage(PeopleQuery peopleQuery) {
        String name = peopleQuery.getName();
        Integer status = peopleQuery.getStatus();

        Page<People> page = Page.of(peopleQuery.getPageNo(), peopleQuery.getPageSize());
        if(StrUtil.isNotBlank(peopleQuery.getSortBy())){
            page.addOrder(new OrderItem(peopleQuery.getSortBy(), peopleQuery.getIsAsc()));
        }else{
            page.addOrder(new OrderItem("update_time", false));
        }
        Page<People> p = lambdaQuery()
                .like(name != null, People::getUsername, name)
                .eq(status != null, People::getStatus, status)
                .page(page);

        PageDTO<PeopleVO> pageDTO = new PageDTO<>();
        pageDTO.setPages(p.getPages());
        pageDTO.setTotal(p.getTotal());
        List<People> records = p.getRecords();
        if(CollUtil.isEmpty(records)){
            pageDTO.setList(Collections.emptyList());
            return pageDTO;
        }
        List<PeopleVO> peopleVOS = BeanUtil.copyToList(records, PeopleVO.class);
        pageDTO.setList(peopleVOS);
        return pageDTO;
    }
}
